package com.ysalliance.getFan.organization.ui;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ysalliance.getFan.organization.Bean.ClassBean;
import com.ysalliance.getFan.organization.Bean.SearchVideoBean;
import com.ysalliance.getFan.organization.Bean.UserBean;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.adapter.ManagerAdminAdapter;
import com.ysalliance.getFan.organization.myview.dialog.ChooseDialog;
import com.ysalliance.getFan.organization.network.Network;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;


/**
 * 搜索管理员
 * @author lucas
 * created at 2020-02-24 14:54
 */
public class SearchAdminAddActivity extends LucasBaseActivity {

    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.tv_search)
    TextView tv_search;
    @BindView(R.id.recycle)
    RecyclerView recycle;




    private ManagerAdminAdapter managerAdminAdapter;
    public List<UserBean> list = new ArrayList<>();

    public ClassBean classBean;

    @Override
    public int bindContentView() {
        return R.layout.activity_search_admin;
    }

    @Override
    public void afterView() {
        super.afterView();


        classBean = (ClassBean) getIntent().getSerializableExtra("class_info");



        //是搜索手机号添加管理员的
        et.setHint("输入手机号");


        et.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        KeyboardUtils.showSoftInput(et);






        //列表Adapter初始化z
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(linearLayoutManager);
        //设置列表默认动画效果
        recycle.setItemAnimator(new DefaultItemAnimator());
        managerAdminAdapter = new ManagerAdminAdapter(R.layout.item_manager_admin, list);
        recycle.setAdapter(managerAdminAdapter);
        //列表点击事件
        managerAdminAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.tv_add){

                    ChooseDialog chooseDialog = new ChooseDialog(mContext,"取消", "确定", "班级管理员拥有在班级内创建任务、点评学员打卡、删除学员的权限。确定添加他为管理员吗？"){
                        @Override
                        public void left() {
                            super.left();
                        }

                        @Override
                        public void right() {
                            super.right();
                            add_admin(classBean.getId(), list.get(position).getUser_id());
                        }
                    };
                    chooseDialog.show();


                }
            }
        });





        initClick();



    }


    public void initClick() {

        tv_search.setOnClickListener(v->{
            search_admin(et.getText().toString().trim());
        });


        //点击搜索
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //执行搜索方法
                    //是搜索手机号添加管理员的
                    et.setHint("输入手机号");
                    search_admin(et.getText().toString().trim());


                }
                return false;
            }
        });


    }


    //隐藏软键盘
    @Override
    protected void onPause() {
        super.onPause();
        KeyboardUtils.hideSoftInput(et);
    }






    /**  获取管理员的   */
    public void search_admin(String class_id){
        disposable = Network.getUtilApi()
                .search_admin(class_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json(" 获取管理员的返回", res);


                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            Gson gson = new Gson();
                            JSONObject jsonObjectData = new JSONObject(  jsonObject.optString("data")  );
                            UserBean userBean = gson.fromJson(jsonObject.optString("data"), UserBean.class);


                            //更新列表
                            list.clear();
                            list.add(userBean);
                            managerAdminAdapter.notifyDataSetChanged();

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }


                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }


    /**  添加管理员的   */
    public void add_admin(String class_id, String user_id){
        disposable = Network.getUtilApi()
                .add_admin(class_id, user_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json(" 添加管理员的返回", res);


                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            ToastUtils.showShort("添加成功");
                            onBackPressed();
                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }


                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }




}
